package com.wanggsx.library.util.fun.dialog;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.graphics.Color;

import com.wanggsx.library.util.UtilsSPLogin;
import com.wanggsx.library.util.UtilsToast;

public class UtilsShowLoginDialog {
    //登录提示框
    public static AlertDialog mDialog;
    //最后显示的activity
    private static Activity mLastActivity;

    /** 监听器对象 */
    public interface OnConfirmOrCancelListener{
        void onCancel();
        void onConfirm();
    }

    public static void toReLogin(Activity activity,OnConfirmOrCancelListener listener) {
        toReLogin(activity,true,false,listener);
    }

    public static void toReLogin(Activity activity,boolean isRelogin,OnConfirmOrCancelListener listener) {
        toReLogin(activity,isRelogin,false,listener);
    }

    public static void toReLogin(Activity activity,boolean isRelogin,boolean cancelable,OnConfirmOrCancelListener listener) {
        String message;
        if (isRelogin){
            message = "您当前登录账号异常，需要重新登录！";
        }else{
            message = "当前功能需要登录才能继续，确定要登录吗？";
        }
        toReLogin(activity,message,cancelable,listener);
    }

    public static void toReLogin(Activity activity,String message,boolean cancelable,OnConfirmOrCancelListener listener){
        toReLogin(activity,"温馨提示",message,"取消","去登录",cancelable,listener);
    }

    public static void toReLogin(Activity activity, String tittle, String message, String cancelText, String confirmText, boolean cancelable, final OnConfirmOrCancelListener listener){
        if (activity==null){
            UtilsToast.showShort("请传递activity");
            return;
        }
        if (listener==null){
            UtilsToast.showShort("请传递监听器对象OnConfirmOrCancelListener");
            return;
        }
        //清理登录状态
        UtilsSPLogin.clearUser();
        //关闭对话框
       /* if (mDialog!=null && activity!=null){
            mDialog.dismiss();
            mDialog = null;
        }*/
       // mLastActivity = activity;
        //创建对话框
        mDialog = new AlertDialog.Builder(activity)
                .setTitle(tittle)
                .setMessage(message)
                .setCancelable(cancelable)
                .setPositiveButton(confirmText, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        mDialog.dismiss();
                        listener.onConfirm();
                    }
                })
                .setNegativeButton(cancelText,new DialogInterface.OnClickListener(){
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        mDialog.dismiss();
                        listener.onCancel();
                    }
                }).create();
        mDialog.show();
        mDialog.getButton(AlertDialog.BUTTON_POSITIVE).setTextColor(Color.parseColor("black"));
        mDialog.getButton(AlertDialog.BUTTON_NEGATIVE).setTextColor(Color.parseColor("black"));
    }

}
